//
//  FKloginModule.swift
//  FireKnowledge
//
//  Created by 杨伟毅 on 2019/4/19.
//  Copyright © 2019 wely. All rights reserved.
//

import Foundation
import UIKit
import SwiftyUserDefaults


class FKloginModule: NSObject {
    static let shared = FKloginModule()
    var timer:Timer?
    var count:Int = 60
    var button:UIButton?
}
extension FKloginModule{
   fileprivate func fire(_ sender:UIButton){
        sender.isEnabled = false
        button = sender
        count = 60
        timer?.invalidate()
        timer = nil
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerFire), userInfo: nil, repeats: true)
        timer?.fire()
    }
    @objc  func timerFire(){
        button?.titleLabel?.text = ""
        count -= 1
        if let _ = button{
            if count <= 0 {
                button?.setTitle("获取验证码", for: .normal)
                button?.isEnabled = true
                timer?.invalidate()
                timer = nil
            }else{
                button?.setTitle("\(count)s", for: UIControl.State.normal)
                button?.isEnabled = false
            }
        }
    }
  fileprivate  func deinitFire()
    {
        count = 0
        timer?.invalidate()
        timer = nil
    }
    class func deinitFire() {
        shared.deinitFire()
    }
    class func fire(_ sender:UIButton){
        shared.fire(sender)
    }
    
    fileprivate func isValidateMobile(moble:String) -> Bool{
        let MOBILE = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$"
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@", MOBILE)
        if regextestmobile.evaluate(with: moble) == true {
            return true
        }else{
            return false
        }
    }
    class func isValidateMobile(moble:String) -> Bool {
       return shared.isValidateMobile(moble: moble)
    }
    fileprivate func  islogin() -> Bool{
        if (Defaults[.userIdSave]?.isEmpty) != nil {
            return true
        }else{
            return false
        }
    }
    class  func islogin() ->Bool {
      return  shared.islogin()
    }
}



